package com.qc.blogsystemplus.service;

import com.qc.blogsystemplus.bean.ArticleBean;
import com.qc.blogsystemplus.bean.CategoryBean;
import com.qc.blogsystemplus.entity.Article;
import com.qc.blogsystemplus.entity.User;
import com.qc.blogsystemplus.mapper.ArticleMapper;
import com.qc.blogsystemplus.mapper.CategoryMapper;
import com.qc.blogsystemplus.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    public List<Article> getAllArticle(){
        return articleMapper.getAllArticle();
    }

    public int addArticle(Article article,String username) {
        User user = userMapper.getUser(username);
        ArticleBean articleBean = new ArticleBean();
        articleBean.setAuthor_userid(user.getId());
        articleBean.setContent(article.getContent());
        articleBean.setTitle(article.getTitle());
        CategoryBean categoryBean = categoryMapper.getCagegory(article.getCategory_name());
        articleBean.setCategory_id(categoryBean.getId());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        articleBean.setPublic_time(df.format(new Date()));
        int re = articleMapper.addArticle(articleBean);
        return re;
    }

    public int delArticle(int id){
        return articleMapper.delArticle(id);
    }

    public List<Article> getArticleByUser(String username) {
        return articleMapper.getArticleByUser(username);
    }

    public Article getArticle(int id) {
        return articleMapper.getArticleByArticleId(id);
    }

    public List<Article> getArticleByCate(String category) {
        return articleMapper.getArticleByCate(category);
    }
}
